﻿<%@ Page Language="C#" %> <%--AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="adolsignweb._Default" %>--%>

<%@ Assembly Name="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"  %>

<%@ Import Namespace="System.DirectoryServices" %>

<%--<%@ Reference  %>--%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        string defaultNamingContext;
        using (DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE"))
        {
            defaultNamingContext = rootDSE.Properties["defaultNamingContext"].Value.ToString();
            //if (cmdShowEnvironment) ListThePropertiesOfRootDSE(rootDSE);
        }

        string path = "LDAP://CN=Users," + defaultNamingContext;
        using (DirectoryEntry root = new DirectoryEntry(path))//, "xx\\xxxx", "xxxx"))
        {
            SearchResult result = GetCurrentActiveDirectoryUser(root);

            StringBuilder userInfo = new StringBuilder();
            userInfo.AppendLine("<script language=\"javascript\" type=\"text/javascript\">");
            userInfo.AppendLine("var userinfovalues = new Array();");
            string result_content = "";
            foreach (string myKey in result.Properties.PropertyNames)
            {
                string tab = "    ";
                //Console.WriteLine(myKey + " = ");
                result_content += myKey + " = ";// +"<br/>";//Environment.NewLine;
                foreach (Object myCollection in result.Properties[myKey])
                {
                    //Console.WriteLine(tab + myCollection);
                    result_content += tab + myCollection + "<br/>";//Environment.NewLine;
                    userInfo.AppendLine(String.Format("userinfovalues['{0}'] = '{1}';", myKey, myCollection.ToString()));
                }//end foreach
            } //end foreach
            userInfo.AppendLine("</"+"script>");

            AdUserInfo.InnerHtml = result_content;

            //Page.ClientScript.RegisterArrayDeclaration("UserInfoValues", "\"1\"");
            //Page.ClientScript.RegisterArrayDeclaration("UserInfoValues", "\"2\"");
            //Page.ClientScript.RegisterArrayDeclaration("UserInfoValues", "\"3\"");
            Page.ClientScript.RegisterStartupScript(this.GetType(), "userinfoobject", userInfo.ToString());// "var userinfovalues = new Array();");
        }
    }

    static SearchResult GetCurrentActiveDirectoryUser(DirectoryEntry root)
    {
        DirectorySearcher ds = new DirectorySearcher(root);
        System.Security.Principal.WindowsIdentity winIdent = System.Security.Principal.WindowsIdentity.GetCurrent();
        string userName = winIdent.Name;
        int hasDomain = userName.IndexOf(@"\");
        if (hasDomain > 0)
        {
            userName = userName.Remove(0, hasDomain + 1);
        }

        //string userFilter = "(&(objectCategory=user)(sAMAccountName={0}))";
        string userFilter = "(&(objectClass=user)(sAMAccountName={0}))";

        ds.SearchScope = SearchScope.Subtree;
        //ds.PropertiesToLoad.Add("distinguishedName");
        //ds.PageSize = 1;
        //ds.ServerPageTimeLimit = TimeSpan.FromSeconds(2);
        ds.Filter = string.Format(userFilter, userName);

        SearchResult sr = ds.FindOne();
        // now do something with sr.Properties["distinguishedName"][0]        

        ds.Dispose();
        return sr;
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
<div id="AdUserInfo" runat="server"></div>

    </div>
    </form>




</body>

<script language="javascript" type="text/javascript">

//    var var1 = new Array();
//    var1['str0'] = '0';
//    var1['str1'] = '1';
//    var1['str2'] = '2';

</script>

</html>
